package be.kuleuven.thesis.parsers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VariantList extends ArrayList<VariantFeature> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public VariantList() {
	}
	
	public VariantList(Collection<VariantFeature> variants){
		this();
		this.add(variants);
	}
	
	public boolean add(VariantFeature variant){
		if(variantIndex.containsKey("chrom")){
			Map<String, List<VariantFeature>> var = variantIndex.get("chrom");
			if(var == null){
				var = new HashMap<String, List<VariantFeature>>();
			}
			List<VariantFeature> variants = var.get(variant.chrom());
			if(variants == null){
				variants = new ArrayList<VariantFeature>();
			}
			variants.add(variant);
			var.put(variant.chrom(),variants);
			variantIndex.put("chrom",var);
		}
		
		if(variantIndex.containsKey("sample")){
			Map<String, List<VariantFeature>> var = variantIndex.get("sample");
			if(var == null){
				var = new HashMap<String, List<VariantFeature>>();
			}
			for (String sample : variant.getSampleNames()){
				List<VariantFeature> variants = var.get(sample);
				if(variants == null){
					variants = new ArrayList<VariantFeature>();
				}
				variants.add(variant);
				var.put(sample, variants);
			}
			variantIndex.put("sample", var);
		}
		return super.add(variant);	
	}
	
	public void add(Collection<VariantFeature> list){
		for (VariantFeature v : list){
			add(v);
		}
	}
	
	Map<String, Map<String,List<VariantFeature>>> variantIndex = new HashMap<String,Map<String,List<VariantFeature>>>();

	public void addIndexes(List<String> indexes) {
		for (String index : indexes){
			addIndex(index);
		}
	}
	
	public void addIndex(String index) {
			variantIndex.put(index, null);
	}	
}
